﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Nexxo.Channel.DMS.Web.Models;
using Nexxo.Channel.DMS.Web.DMSServices;
using System.Collections;
using System.ServiceModel;
using Nexxo.Channel.DMS.Web.Common;

namespace Nexxo.Channel.DMS.Web.Controllers
{
    public class ProfileSummaryController : BaseController
    {
        /// <summary>
        /// Something about what the <c>MySomeFunction</c> does
        /// with some of the sample like
        /// <code>
        /// Some more code statement to comment it better
        /// </code>
        /// For more information seee <see cref="http://www.me.com"/>
        /// </summary>
        /// <param name="someObj">What the input to the function is</param>
        /// <returns>What it returns</returns>
        //public ActionResult Index()
        //{
        //    return View();
        //}

        /// <summary>
        /// Something about what the <c>MySomeFunction</c> does
        /// with some of the sample like
        /// <code>
        /// Some more code statement to comment it better
        /// </code>
        /// For more information seee <see cref="http://www.me.com"/>
        /// </summary>
        /// <param name="someObj">What the input to the function is</param>
        /// <returns>What it returns</returns>
        public ActionResult Create()
        {
            DateTime dt = DateTime.Now;
            ProfileSummary PS = new ProfileSummary();
            if (Session["CurrentProspect"] != null)
            {
                Prospect prospect = (Prospect)Session["CurrentProspect"];
                PS.Name = prospect.FName + " " + prospect.MName + " " + prospect.LName;
                PS.Gender = prospect.Gender;
                PS.PrimaryPhone = prospect.Phone1;
                PS.Email = prospect.Email;
                PS.Address = string.Join(",", new string[] { prospect.Address1, prospect.Address2, prospect.City, prospect.State });
                
                PS.Profession = prospect.Occupation;
                PS.EmployerName = prospect.Employer;
                PS.EmployerPhoneNumber = prospect.EmployerPhone;

                PS.MotherMaidenName = prospect.MoMaName;
                PS.DateofBirth = prospect.DOB.ToShortDateString();

                if (prospect.ID != null)
                {
                    PS.Country = prospect.ID.Country;
                    PS.GovtIDType = prospect.ID.IDType;
                    PS.GovtIdIssueState = prospect.ID.State;
                    PS.IDNumber = prospect.ID.ID;
                    PS.IDExpirationDate = prospect.ID.ExpirationDate.ToShortDateString();
                }
            }

            return View(PS);
        }

        /// <summary>
        /// Something about what the <c>MySomeFunction</c> does
        /// with some of the sample like
        /// <code>
        /// Some more code statement to comment it better
        /// </code>
        /// For more information seee <see cref="http://www.me.com"/>
        /// </summary>
        /// <param name="someObj">What the input to the function is</param>
        /// <returns>What it returns</returns>
        [HttpPost]
        public ActionResult Create(ProfileSummary PSumm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Prospect prospect = (Prospect)Session["CurrentProspect"];

                    PSumm.Name = prospect.FName + " " + prospect.MName + " " + prospect.LName;
                    PSumm.Gender = prospect.Gender;
                    PSumm.PrimaryPhone = prospect.Phone1;
                    PSumm.Email = prospect.Email;
                    PSumm.Address = string.Join(",", new string[] { prospect.Address1, prospect.Address2, prospect.City, prospect.State });

                    PSumm.Profession = prospect.Occupation;
                    PSumm.EmployerName = prospect.Employer;
                    PSumm.EmployerPhoneNumber = prospect.EmployerPhone;

                    PSumm.MotherMaidenName = prospect.MoMaName;
                    PSumm.DateofBirth = prospect.DOB.ToShortDateString();
                    PSumm.Country = prospect.ID.Country;
                    PSumm.GovtIDType = prospect.ID.IDType;
                    PSumm.GovtIdIssueState = prospect.ID.State;
                    PSumm.IDNumber = prospect.ID.ID;
                    PSumm.IDExpirationDate = prospect.ID.ExpirationDate.ToShortDateString();


                    DesktopServiceClient desktopserviceclient = new DesktopServiceClient();
                    desktopserviceclient.Save(((Hashtable)Session["HTSessions"])["AgentSessionId"].ToString(), Session["PAN"].ToString(), prospect);
                    desktopserviceclient.Activate(((Hashtable)Session["HTSessions"])["AgentSessionId"].ToString(), Session["PAN"].ToString());

                    Session["PAN"] = null;
                    Session["CurrentProspect"] = null;

                    Session.Remove("PAN");
                    Session.Remove("CurrentProspect");


                    return RedirectToAction("Create", "LandingPage");
                }
                return View(PSumm);
            }
            catch (FaultException<NexxoSOAPFault> nexxoFault)
            {
                ViewBag.ExceptionMessage = ExceptionHandler.GetSOAPExceptionMessage(nexxoFault);
                return View(PSumm);
            }
            catch (Exception e)
            { 
                ViewBag.ExceptionMessage = ExceptionHandler.GetExceptionMessage(e);
                return View(PSumm);
            }

        }
    }
}
